package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.sys.YsDict;
import com.ys.entity.sys.YsModalSelector;
import com.ys.entity.sys.YsModalSelectorField;
import com.ys.enume.sys.YsModalSelectorFieldEnum;
import com.ys.mapper.sys.SysMapper;
import com.ys.mapper.sys.YsModalSelectorFieldMapper;
import com.ys.service.sys.YsModalSelectorFieldService;
import com.ys.vo.R;
import com.ys.vo.W;

@Service("ysModalSelectorFieldService")
public class YsModalSelectorFieldServiceImpl implements YsModalSelectorFieldService {

	@Autowired
	private SysMapper sysMapper;
	@Autowired
	private YsModalSelectorFieldMapper ysModalSelectorFieldMapper;

	@Override
	public DtreeSqlDto dtree() {
		
		DtreeSqlDto.StatusDto status = DtreeSqlDto.StatusDto.init(200, "");

		List<DtreeSqlDto.DataDto> data = new ArrayList<DtreeSqlDto.DataDto>();
		List<YsModalSelector> listYsModalSelector = new YsModalSelector()
				.setOrderby("A.Id asc")
				.queryList();
		
		// 一级
		for(YsModalSelector dom1 : listYsModalSelector) {
			String id1 = dom1.getId().toString();
			String title1 = dom1.getName() + "(" + dom1.getNameTable() + ")";
			String parentId1 = null;
			
			// 二级
			List<DtreeSqlDto.DataDto> children = new ArrayList<DtreeSqlDto.DataDto>();
			
			DtreeSqlDto.DataDto dataDto1 = new DtreeSqlDto.DataDto(id1, title1, parentId1, DtreeSqlDto.DataDto.initCheckArr(null), children);
			data.add(dataDto1);
		}
		return new DtreeSqlDto(status, data);
	}

	@Override
	public R add(Long pid,
			String code,
			String name,
			String type,
			Integer sortNo,
			Integer stateCondition,
			String conditionCode,
			String dictKeyBase,
			Integer align,
			Integer width) {
		
		YsModalSelectorField entity = YsModalSelectorField.ysModalSelectorField(pid, code, name, type, sortNo, stateCondition, conditionCode, dictKeyBase, align, width, 1);
		
		ysModalSelectorFieldMapper.insert(entity);
		
		return R.ok(AddDto.closeAndReload());
	}

	@Override
	public R edit(Long id, String name, String code, String type, Integer stateCondition, String conditionCode) {
		
		YsModalSelectorField entity = new YsModalSelectorField().setId(id).queryFirst();
		entity
			.setName(name)
			.setCode(code)
			.setType(type)
			.setStateCondition(stateCondition);
		
		ysModalSelectorFieldMapper.update(entity);
		
		return R.ok();
	}

	@Override
	@Transactional
	public R delete(Long id) {
		if (id == null) {
			return R.error("ID不能为空");
		}
		ysModalSelectorFieldMapper.deleteTrue(id);
		
		return R.ok();
	}

	@Override
	public String toResort(Model model, Long pid) {
		
		List<YsModalSelectorField> listEntity = new YsModalSelectorField().setPid(pid).setOrderby("A.SortNo asc").queryList();;
		
		List<ToResortDto.RowDto> rows = new ArrayList<ToResortDto.RowDto>();
		for(YsModalSelectorField entity : listEntity) {
			Long id = entity.getId();
			String name = entity.getName();
			rows.add(new ToResortDto.RowDto(id, name));
		}
		
		ToResortDto dto = new ToResortDto( rows);
		model.addAttribute("dto", dto);
		return "sys/ysModalSelectorField/toResort";
	}

	@Override
	@Transactional
	public R resort(String rows) {
		JSONArray JA = JSON.parseArray(rows);
		
		for (int i = 0; i < JA.size(); i++) {
			JSONObject JO = JA.getJSONObject(i);
			Long id = JO.getLong("id");
			Integer fieldid = i + 1;
			sysMapper.updateSortNoById(new YsModalSelectorField().generateTableAllName(), id, fieldid);
		}
		return R.ok();
	}

	@Override
	public String toOptionSetting(Model model, Long id) {
		
		if (id == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsModalSelectorField entity = new YsModalSelectorField().setId(id).queryFirst();
		if (!YsModalSelectorFieldEnum.Type.SELECTOR_DROPDOWN.value.equals(entity.getType())
				&& !YsModalSelectorFieldEnum.Type.SELECTOR_SWITCH.value.equals(entity.getType())) {
			return W.error(model, "非下拉框类型无法设置");
		}
		
		// 字典数组
		List<ToOptionConfigDto.RowDto> rows = new ArrayList<ToOptionConfigDto.RowDto>();
		String dictKey = entity.getDictKeyBase();
		if (dictKey != null && dictKey.length() > 0) {
			
			List<YsDict> listYsDict = new YsDict().setDictKey(dictKey)
					.setOrderby("A.SortNo asc")
					.queryList();
			
			for(YsDict ysDict : listYsDict) {
				Long id2 = ysDict.getId();
				String dictName = ysDict.getDictName();
				String dictValue = ysDict.getDictValue();
				rows.add(new ToOptionConfigDto.RowDto(id2, dictName, dictValue));
			}
		}
		
		String name = entity.getName();
		String code = entity.getCode();
		ToOptionConfigDto dto = new ToOptionConfigDto(id, name, code, dictKey, rows);
		model.addAttribute("dto", dto);
		return "sys/ysModalSelectorField/toOptionSetting";
	}

	@Override
	public R optionSetting(Long id, String dictKey) {
		
		if (id == null) {
			return R.error("ID不能为空");
		}
		
		YsModalSelectorField entity = new YsModalSelectorField().setId(id).queryFirst();
		if (entity == null) {
			return R.error("数据不存在");
		} else if (!YsModalSelectorFieldEnum.Type.SELECTOR_DROPDOWN.value.equals(entity.getType())
				&& !YsModalSelectorFieldEnum.Type.SELECTOR_SWITCH.value.equals(entity.getType())) {
			return R.error("非选择框类型的数据");
		}
		
		entity.setDictKeyBase(dictKey);
		ysModalSelectorFieldMapper.update(entity);
		return R.ok();
	}
}
